package com.shu.Algorithm;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.stream.IntStream;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2021/8/13 18:03
 */

public class Test1 {

    public static void main(String[] args) {
        System.out.println("[0,9]       : " + new Test1().countDigitOne(9));
        System.out.println("[0,99]      : " + new Test1().countDigitOne(99));
        System.out.println("[0,999]     : " + new Test1().countDigitOne(999));
        System.out.println("[0,9999]    : " + new Test1().countDigitOne(9999));
        System.out.println("[0,99999]   : " + new Test1().countDigitOne(99999));
    }

    public int countDigitOne(int n) {
        final int[] x = {0};
        IntStream.range(0, n + 1).forEach(y -> {
            long count = Arrays.stream((y + "0").split("1")).filter(StringUtils::isBlank).count();
            x[0] += count;
        });
        return x[0];
    }
}
